package com.zl.controller;

import com.zl.dao.OrderDao;
import com.zl.dao.OrderDetailDao;
import com.zl.pojo.Order;
import com.zl.pojo.OrderDetail;
import com.zl.pojo.User;
import com.zl.service.OrderDetailService;
import com.zl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BuyCarController {
    @Autowired
    private OrderDetailService ods;
    @Autowired
    private OrderService os;
    @RequestMapping("/queryId.do")
    public String queryId(HttpSession session, Map<String,Object> maps){
        User user= (User) session.getAttribute("user");
        List<Order> orders= os.queryOrderByUserId(user.getId());
        Order oo= orders.get(0);
        List<OrderDetail> order=ods.queryById(oo.getId());
        Double cost=ods.queryOrderCostByOrderId(oo.getId());
        System.out.println("总消费："+cost);
        maps.put("cost",cost);
        maps.put("order",order);//将查询到的商品列表返回到前台
        maps.put("user",user);

        return "/buyCar/buyCar";
    }
    @RequestMapping("/updateQuantity.do")
    @ResponseBody
    public Map<String,Object> updateQuantity( Integer quantity, Integer productId,HttpSession session,Map<String ,Object> maps){
        System.out.println("进入添加方法");
        System.out.println("产品的编号为："+productId);
        //获取当前用户
        User user= (User) session.getAttribute("user");
        //查询用户地址信息
        List<Order> orders= os.queryOrderByUserId(user.getId());
        Order oo= orders.get(0);
        //获取商品订单列表
        List<OrderDetail> order=ods.queryById(oo.getId());
        //得到要修改的商品
        OrderDetail odd=ods.query(oo.getId(),productId);
        System.out.println(odd);
        //设置商品数量
        System.out.println(quantity);
        odd.setQuantity(quantity);
        //修改商品数量
        int index=ods.updateODD(odd);
        System.out.println("修改结果："+index);
        OrderDetail odd1=ods.query(oo.getId(),productId);
        maps.put("index",index);
        maps.put("odd1",odd1);
        return maps;
    }
    @RequestMapping("/delete.do")
    public String delete(OrderDetail odd,HttpServletRequest request){
        request.setAttribute("odd",odd);
        return "redirect:/deleting.do";
    }
    @RequestMapping("/deleting.do")
    public String deleting(OrderDetail odd){
        int result=ods.delete(odd);
        System.out.println(result);
        return "/buyCar/buyCar";
    }
}
